package com.leyou.item.service;

import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecificationService {

    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;

    public List<SpecGroup> querySpecGroupsByCid(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        return specGroupMapper.select(specGroup);
    }

    public void saveSpecGroups(SpecGroup specGroups) {
        specGroupMapper.insert(specGroups);
    }

    public void updateSpecGroups(SpecGroup specGroups) {
        specGroupMapper.updateByPrimaryKeySelective(specGroups);
    }

    public void deleteSpecGroups(Long id) {
        specGroupMapper.deleteByPrimaryKey(id);
    }

    public void deleteSpecParam(Long id) {
        specParamMapper.deleteByPrimaryKey(id);
    }

    public void updateSpecParam(SpecParam specParam) {
        specParamMapper.updateByPrimaryKeySelective(specParam);
    }

    public void saveSpecParam(SpecParam specParam) {
        specParamMapper.insert(specParam);
    }

    public List<SpecParam> querySpecParams(Long gid, Long cid, Boolean searching, Boolean generic) {
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);
        specParam.setGeneric(generic);
        return specParamMapper.select(specParam);
    }

    public List<SpecGroup> queryGroupWithParamByCid(Long cid) {
        List<SpecGroup> groups = this.querySpecGroupsByCid(cid);

        groups.forEach(group->{
            group.setParams(this.querySpecParams(group.getId(), null, null, null));
        });
        return groups;
    }
}
